package com.masterclient.app.modules.shop.adapter;

import android.graphics.Color;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.masterclient.app.R;
import com.masterclient.app.modules.shop.beans.StockDetailsBean;

public class StockDetailsItemAdapter extends BaseQuickAdapter<StockDetailsBean.SkuItemBean, BaseViewHolder> {

    public StockDetailsItemAdapter() {
        super(R.layout.item_stock_details);
    }

    @Override
    protected void convert(BaseViewHolder helper, StockDetailsBean.SkuItemBean item) {
        TextView tvHomeStock = helper.getView(R.id.tv_home_stock);
        helper.setText(R.id.tv_title, item.attribute);
        helper.setText(R.id.tv_all_stock, item.totalStock);
        tvHomeStock.setText(item.homeStock);
        if (item.isDealerGift || Integer.parseInt(item.homeStock) == 0) {
            tvHomeStock.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        } else {
            tvHomeStock.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.icon_stock_edit, 0);
            helper.addOnClickListener(R.id.view_home_stock);
        }
        helper.setText(R.id.tv_other_stock, item.storeStock);

        helper.setText(R.id.tv_home_stock_des, getSpannableString(item.holdQty));
        helper.setText(R.id.tv_other_stock_des, getSpannableString(item.custodyHoldQty));
    }

    private SpannableString getSpannableString(String content) {
        if (TextUtils.isEmpty(content)) {
            return new SpannableString("");
        } else {
            String frontTitle = "(占用库存 ";
            String endTitle = "不可用)";
            String homeHoleStr = frontTitle + content + endTitle;
            SpannableString spannableString = new SpannableString(homeHoleStr);
            spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#E62E2E")), frontTitle.length(), frontTitle.length() + content.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
            return spannableString;
        }
    }
}
